package com.yimixian.app.ui;

import com.yimixian.app.R;
import com.yimixian.app.goods.BannersView;
import com.yimixian.app.goods.GoodsViewModel;
import com.yimixian.app.model.Banner;
import com.yimixian.app.model.HeadInfo;
import com.yimixian.app.model.HomeDataBean;
import com.yimixian.app.model.ImageInfo;
import com.yimixian.app.model.Material;
import com.yimixian.app.rest.api.ResponseDataTypeAPI;
import com.yimixian.app.util.GlobalLayoutUtils;
import com.ymx.sdk.util.DensityUtils;
import com.ymx.sdk.util.DeviceUtils;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by zhilian-2 on 2015/10/15.
 */
public class HomeHeadView extends LinearLayout {

    int screenWidth;
    public interface OnHeadViewClickListener {
        void OnHeadViewClick(Enum type, int position);
    }

    private OnHeadViewClickListener mHeadViewClickListener;

    public HomeHeadView(Context context) {
        super(context);
        initView();
    }

    public HomeHeadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public HomeHeadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private ScrollPlayView scrollPlayView;

    public ScrollPlayView getScrollPlayView() {
        return scrollPlayView;
    }

    void initView(){
        this.setOrientation(LinearLayout.VERTICAL);
        setBackgroundColor(getResources().getColor(R.color.app_background_grey));
        screenWidth = DeviceUtils.getScreenWidth(getContext());
    }

    public void bindData(HomeDataBean homeDataBean) {
        Map<String, Material> dataMap = homeDataBean.dataMap;
        if (dataMap == null ) return;

        List<Material> headLists = new ArrayList<>();
        if (dataMap.get(ResponseDataTypeAPI.HomeDataType.METROS) != null)
            headLists.add(dataMap.get(ResponseDataTypeAPI.HomeDataType.METROS));
        if (dataMap.get(ResponseDataTypeAPI.HomeDataType.SLIDES) != null)
            headLists.add(dataMap.get(ResponseDataTypeAPI.HomeDataType.SLIDES));
        if (dataMap.get(ResponseDataTypeAPI.HomeDataType.BANNERS) != null)
            headLists.add(dataMap.get(ResponseDataTypeAPI.HomeDataType.BANNERS));
        if (dataMap.get(ResponseDataTypeAPI.HomeDataType.SETHEADLINE) != null)
            headLists.add(dataMap.get(ResponseDataTypeAPI.HomeDataType.SETHEADLINE));
        Collections.sort(headLists);

        for(int i = 0; i < headLists.size(); i++){
            if (headLists.get(i) != null && headLists.get(i).mList!= null &&
                    headLists.get(i).mList.size() > 0 && !StringUtils.isEmpty(headLists.get(i).type)){
                if (ResponseDataTypeAPI.HomeDataType.SLIDES.equals(headLists.get(i).type)){
                    float ratio = ((Material<Banner>)headLists.get(i)).mList.get(0).image_ratio;
                    GoodsViewModel model = new GoodsViewModel(headLists.get(i).mList,
                            ratio == 0 ? 3 : ratio, null);
                    View bannersView = getBannersView(model);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            (int)(screenWidth / (ratio == 0 ? 3 : ratio))
                    );
                    this.addView(bannersView, params);
                }else if (ResponseDataTypeAPI.HomeDataType.METROS.equals(headLists.get(i).type)){
                    float ratio = ((Material<ImageInfo>)headLists.get(i)).mList.get(0).image_ratio;
                    MetroWrapView metroWrapView = new MetroWrapView(getContext());
                    metroWrapView.bind(((Material<ImageInfo>)headLists.get(i)).mList,
                            ratio, mHeadViewClickListener);
                    this.addView(metroWrapView);
                }else if (ResponseDataTypeAPI.HomeDataType.BANNERS.equals(headLists.get(i).type)){
                    float ratio = ((Material<ImageInfo>)headLists.get(i)).mList.get(0).image_ratio;
                    SingleBannerLinearLayout mSingleBanner = new SingleBannerLinearLayout(getContext());
                    mSingleBanner.bind(((Material<ImageInfo>)headLists.get(i)).mList,
                            ratio == 0 ? 2.88f : ratio, mHeadViewClickListener);
                    this.addView(mSingleBanner);
                }else if (ResponseDataTypeAPI.HomeDataType.SETHEADLINE.equals(headLists.get(i).type)){
                    scrollPlayView = new ScrollPlayView(getContext());
                    scrollPlayView.bind(((Material<HeadInfo>) headLists.get(i)).mList, mHeadViewClickListener);
                    scrollPlayView.play(3000);
                    this.addView(scrollPlayView);
                }
            }
        }

        if (getChildCount() > 0){
            View view = new View(getContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, DensityUtils.dp2px(getContext(), 5));
            view.setLayoutParams(params);
            this.addView(view);
        }
//        //轮播图
//        Material<Banner> materialSliders = dataMap.get(ResponseDataTypeAPI.HomeDataType.SLIDES);
//        if (materialSliders != null && materialSliders.mList != null &&
//                materialSliders.mList.size() > 0){
//            float ratio = materialSliders.mList.get(0).image_ratio;
//            GoodsViewModel model = new GoodsViewModel(materialSliders.mList,
//                    ratio == 0 ? 3 : ratio, null);
//            View bannersView = getBannersView(model);
//            this.addView(bannersView);
//        }

//        //metros
//        Material<ImageInfo> materialMetros = dataMap.get(ResponseDataTypeAPI.HomeDataType.METROS);
//        if (materialMetros != null && materialMetros.mList != null &&
//                materialMetros.mList.size() > 0){
//            float ratio = materialMetros.mList.get(0).image_ratio;
//            MetroWrapView metroWrapView = new MetroWrapView(getContext());
//
//            metroWrapView.bind(materialMetros.mList, screenWidth / 4, mHeadViewClickListener);
//            this.addView(metroWrapView);
//        }

//        //singleBanner
//        Material<ImageInfo> materialSingleBanner = dataMap.get(ResponseDataTypeAPI.HomeDataType.BANNERS);
//        if (materialSingleBanner != null && materialSingleBanner.mList != null &&
//                materialSingleBanner.mList.size() > 0){
//            float ratio = materialSingleBanner.mList.get(0).image_ratio;
//            SingleBannerLinearLayout mSingleBanner = new SingleBannerLinearLayout(getContext());
//            mSingleBanner.bind(materialSingleBanner.mList,
//                    ratio == 0 ? 2.88f : ratio, mHeadViewClickListener);
//            this.addView(mSingleBanner);
//        }
    }

    private View getBannersView(final GoodsViewModel model) {
        final BannersView bannersView = new BannersView(getContext());
        GlobalLayoutUtils.runAfterLayout(bannersView, new Runnable() {
            @Override
            public void run() {
                bannersView.bind(model.banners, mHeadViewClickListener);
            }
        });
//        bannersView.setImageRatio(model.bannersRatio);
        return bannersView;
    }

    public void setOnHeadViewClickListener(OnHeadViewClickListener mHeadViewClickListener) {
        this.mHeadViewClickListener = mHeadViewClickListener;
    }
}
